<template>
	<view class="uni-tab-bar">
		<!-- 第一个tab -->
		<scroll-view id="tab-bar" class="uni-swiper-tab" scroll-x :scroll-left="scrollLeft">
			<view v-for="(tab,index) in tabBars" :key="tab.id" :class="['swiper-tab-list',tabIndex==index ? 'active' : '']" :id="tab.id" :data-current="index" @tap="tapTab">{{tab.name}}</view>
		</scroll-view>
		<!-- 第二个tab -->
		<view style="display: flex;line-height: 80upx;flex-wrap: nowrap;">
			<view
				style="background-color: #fff;height: 80upx;line-height: 80upx;border-bottom: 1upx solid #ccc;flex: 1;text-align: center;"
				v-for="(intem,index1) in inBars" :key="index1"
				:class="[ tabIndex1 == index1 ? 'active' : '']"
				:data-current="index1"
				@tap="tapTab1">{{intem.name}}
			</view>
		</view>
		
		<swiper :current="tabIndex" class="swiper-box" duration="300" @change="changeTab" :style="{'height': systemInfo +'px'}">
			<swiper-item v-for="(tab,index1) in newsitems" :key="index1">
				<scroll-view class="list" scroll-y @scrolltolower="loadMore">
					<block v-if="tabIndex1 == 0"><!-- v-for="(newsitem,index2) in tab.data" :key="index2" -->
						111
					</block>
					<block v-if="tabIndex1 == 1"><!-- v-for="(newsitem,index2) in tab.data_group" :key="index2" -->
						222
					</block>
					<block v-if="tabIndex1 == 2"><!-- v-for="(newsitem,index2) in tab.data_limitTime" :key="index2" -->
						333
					</block>
				</scroll-view>
			</swiper-item>
		</swiper>

		<!-- <uni-load-more :loadingType="loadingType" :contentText="contentText"></uni-load-more> -->
	</view>
</template>
<script>
	// import uniLoadMore from '@/components/uni-load-more.vue';
	export default {
		components: {
			// uniLoadMore
		},
		// 下拉刷新
		// onPullDownRefresh() {
		// 	this.page = 1
		// 	this.loadingType = 0;//将loadingType归0重置
		// 	let o = this
		// 	setTimeout(function () {
		// 		o.setAjax();
		// 		uni.stopPullDownRefresh();
		// 	}, 1000);
		// },
		data() {
			return {
				user_id:'',
				res:[],
				systemInfo:[],
				tabIndex: 0, // 当前点击项 第一栏
				tabIndex1: 0, // 第二栏
				scrollLeft: 0,
				isClickChange: false,
				newsitems: [
					{
						"loadingType":0,
						"data":[], // 普通订单
						"data_group":[], // 拼团订单
						"data_limitTime":[], // 限时订单
					},{
						"loadingType":0,
						"data":[],
						"data_group":[], // 拼团订单
						"data_limitTime":[],
					},{
						"loadingType":0,
						"data":[],
						"data_group":[], // 拼团订单
						"data_limitTime":[],
					},{
						"loadingType":0,
						"data":[],
						"data_group":[], // 拼团订单
						"data_limitTime":[],
					},{
						"loadingType":0,
						"data":[],
						"data_group":[], // 拼团订单
						"data_limitTime":[],
					},
					{
						"loadingType":0,
						"data":[],
						"data_group":[], // 拼团订单
						"data_limitTime":[],
					}
				],
				tabBars: [
					{
						name: '全部',
						id: 'all'
					}, {
						name: '待付款',
						id: 'pending_pay'
					}, {
						name: '待发货',
						id: 'to_be_delivered'
					}, {
						name: '已发货',
						id: 'shipped'
					}, {
						name: '已完成',
						id: 'finished'
					}, {
						name: '已取消',
						id: 'cancled'
					}
				],
				inBars: [
					{
						name: '第一栏'
					},
					{
						name: '第二栏'
					},
					{
						name: '第三栏'
					},
				],
				// loadingText: '加载中...',
				// loadingType: 0,
				// contentText: {
				// 	contentdown: '上拉显示更多',
				// 	contentrefresh: '正在加载...',
				// 	contentnomore: '没有更多数据了'
				// },
				// page: 1,
				// page1: 1,
				// page2: 1,
				// timer:{},
			}
		},
		onLoad: function(e) {
			this.tabIndex = 1
			setTimeout(() => {
				this.tabIndex = e.index
			}, 1000);
			uni.getSystemInfo({
				success: (res) => {
					this.systemInfo = res.windowHeight -50
				}
			})
			uni.getStorage({
				key: 'user_id',
				success: (res) => {
				 this.user_id = res.data
				 // this.setAjax()
				},
				fail() {
					// uni.navigateTo({
					// 	url:'../login/login'
					// })
				}
			});
		},
		methods: {
			setAjax(){
				var o = this
				// 普通订单
				uni.request({
					url: '',
					method: 'POST',
					data: {
						'user_id': this.user_id,
						'session_id': uni.getStorageSync("sessionid")
					},
					header: {'Content-Type': 'application/x-www-form-urlencoded'},
					success: res => {
						let fil = res.data.data.order_list.list
						for(var i = 0; i < fil.length; i++) {
							if (fil[i].goodsInfo) {
								for(var j = 0; j < fil[i].goodsInfo.length; j++) {
									fil[i].goodsInfo[j].goods_thumb_image = 'https://cx.mingmeijt.com' + fil[i].goodsInfo[j].goods_thumb_image
								}
							}
						}
						let fil1 = fil.filter(function(item,index,array){
								return (item.status == "1" || item.status == 1);
						})
						let fil2 = fil.filter(function(item,index,array){
								return (item.status == "2" || item.status == 2);
						})
						let fil3 = fil.filter(function(item,index,array){
								return (item.status == "3" || item.status == 3);
						})
						let fil4 = fil.filter(function(item,index,array){
								return (item.status == "4" || item.status == 4);
						})
						let fil5 = fil.filter(function(item,index,array){
								return (item.status == "5" || item.status == 5);
						})
						this.newsitems[0].data = fil
						this.newsitems[1].data = fil1
						this.newsitems[2].data = fil2
						this.newsitems[3].data = fil3
						this.newsitems[4].data = fil4
						this.newsitems[5].data = fil5
					},
					fail: () => {},
					complete: () => {
					}
				});
				// 拼团订单
				uni.request({
					url: '',
					method: 'POST',
					data:{
						user_id: this.user_id,
						session_id: uni.getStorageSync("sessionid")
					},
					header:{ 'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						let data_group = res.data.data.order_list.list
						for(var i = 0; i < data_group.length; i++) {
							data_group[i].goods_thumb = 'https://cx.mingmeijt.com' + data_group[i].goods_thumb
						}
						let data_group1 = data_group.filter(function(item,index,array){
								return (item.status == "1" || item.status == 1);
						})
						let data_group2 = data_group.filter(function(item,index,array){
								return (item.status == "2" || item.status == 2);
						})
						let data_group3 = data_group.filter(function(item,index,array){
								return (item.status == "3" || item.status == 3);
						})
						let data_group4 = data_group.filter(function(item,index,array){
								return (item.status == "4" || item.status == 4);
						})
						let data_group5 = data_group.filter(function(item,index,array){
								return (item.status == "5" || item.status == 5);
						})
						this.newsitems[0].data_group = data_group
						this.newsitems[1].data_group = data_group1
						this.newsitems[2].data_group = data_group2
						this.newsitems[3].data_group = data_group3
						this.newsitems[4].data_group = data_group4
						this.newsitems[5].data_group = data_group5
					},
					fail: () => {},
					complete: () => {}
				});
				// 限时活动
				uni.request({
					url: '',
					method: 'POST',
					data:{
						'user_id': this.user_id,
						'session_id': uni.getStorageSync("sessionid")
					},
					header:{ 'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						let data_limitTime = res.data.data.order_list.list
						
						for(var i = 0; i < data_limitTime.length; i++) {
							data_limitTime[i].goods_thumb_image = 'https://cx.mingmeijt.com' + data_limitTime[i].goods_thumb_image
							data_limitTime[i].goods_thumb = 'https://cx.mingmeijt.com' + data_limitTime[i].goods_thumb
						}
						
						let data_limitTime1 = data_limitTime.filter(function(item,index,array){
								return (item.status == "1" || item.status == 1);
						})
						let data_limitTime2 = data_limitTime.filter(function(item,index,array){
								return (item.status == "2" || item.status ==2);
						})
						let data_limitTime3 = data_limitTime.filter(function(item,index,array){
								return (item.status == "3" || item.status == 3);
						})
						let data_limitTime4 = data_limitTime.filter(function(item,index,array){
								return (item.status == "4" || item.status == 4);
						})
						let data_limitTime5 = data_limitTime.filter(function(item,index,array){
								return (item.status == "5" || item.status == 5);
						})
						this.newsitems[0].data_limitTime = data_limitTime
						this.newsitems[1].data_limitTime = data_limitTime1
						this.newsitems[2].data_limitTime = data_limitTime2
						this.newsitems[3].data_limitTime = data_limitTime3
						this.newsitems[4].data_limitTime = data_limitTime4
						this.newsitems[5].data_limitTime = data_limitTime5
					},
					fail: () => {},
					complete: () => {}
				});
			},
			// 页面触底 加载更多
			loadMore(e) {
				let o = this
				if (this.loadingType !== 0) {//loadingType!=0;直接返回
					return false;
				}
				this.loadingType = 1;
				uni.showNavigationBarLoading();//显示加载动画
				// 普通订单列表 更多
				if(this.tabIndex1 == 0) {
					this.page++;//每触底一次 page +1
					uni.request({
						url:'https://cx.mingmeijt.com/Api/Order/index?p=' + this.page,
						method: 'POST',
						data: {
							user_id: this.user_id,
							session_id: uni.getStorageSync("sessionid")
						},
						header:{'Content-Type':'application/x-www-form-urlencoded'},
						success: function(res) {
							if (res.data.data.order_list.list == '' || res.data.data.order_list.list == null) {//没有数据
								o.loadingType = 2;
								uni.hideNavigationBarLoading();//关闭加载动画
								return;
							}
							let fil = res.data.data.order_list.list
							for(var i = 0; i < fil.length; i++) {
								if (fil[i].goodsInfo) {
									for(var j = 0; j < fil[i].goodsInfo.length; j++) {
										fil[i].goodsInfo[j].goods_thumb_image = 'https://cx.mingmeijt.com' + fil[i].goodsInfo[j].goods_thumb_image
									}
								}
							}
							let fil1 = fil.filter(function(item,index,array){
									return (item.status == "1" || item.status == 1);
							})
							let fil2 = fil.filter(function(item,index,array){
									return (item.status == "2" || item.status == 2);
							})
							let fil3 = fil.filter(function(item,index,array){
									return (item.status == "3" || item.status == 3);
							})
							let fil4 = fil.filter(function(item,index,array){
									return (item.status == "4" || item.status == 4);
							})
							let fil5 = fil.filter(function(item,index,array){
									return (item.status == "5" || item.status == 5);
							})
							o.newsitems[0].data = o.newsitems[0].data.concat(fil);
							o.newsitems[1].data = o.newsitems[1].data.concat(fil1);
							o.newsitems[2].data = o.newsitems[2].data.concat(fil2);
							o.newsitems[3].data = o.newsitems[3].data.concat(fil3);
							o.newsitems[4].data = o.newsitems[4].data.concat(fil4);
							o.newsitems[5].data = o.newsitems[5].data.concat(fil5);
							o.loadingType = 0;//将loadingType归0重置
							uni.hideNavigationBarLoading();//关闭加载动画
						}
					});
				}
				// 拼团 更多
				if(this.tabIndex1 == 1) {
					this.page1++;//每触底一次 page +1
					uni.request({
						url:'https://cx.mingmeijt.com/Api/AssembleOrder/index?p=' + this.page1,
						method: 'POST',
						data: {
							user_id: this.user_id,
							session_id: uni.getStorageSync("sessionid")
						},
						header:{'Content-Type':'application/x-www-form-urlencoded'},
						success: function(res) {
							if (res.data.data.order_list.list == '' || res.data.data.order_list.list == null) {//没有数据
								o.loadingType = 2;
								uni.hideNavigationBarLoading();//关闭加载动画
								return;
							}
							let data_group = res.data.data.order_list.list
							for(var i = 0; i < data_group.length; i++) {
								data_group[i].goods_thumb = 'https://cx.mingmeijt.com' + data_group[i].goods_thumb
							}
							let data_group1 = data_group.filter(function(item,index,array){
									return (item.status == "1" || item.status == 1);
							})
							let data_group2 = data_group.filter(function(item,index,array){
									return (item.status == "2" || item.status == 2);
							})
							let data_group3 = data_group.filter(function(item,index,array){
									return (item.status == "3" || item.status == 3);
							})
							let data_group4 = data_group.filter(function(item,index,array){
									return (item.status == "4" || item.status == 4);
							})
							let data_group5 = data_group.filter(function(item,index,array){
									return (item.status == "5" || item.status == 5);
							})
							o.newsitems[0].data_group = o.newsitems[0].data_group.concat(data_group);
							o.newsitems[1].data_group = o.newsitems[1].data_group.concat(data_group1);
							o.newsitems[2].data_group = o.newsitems[2].data_group.concat(data_group2);
							o.newsitems[3].data_group = o.newsitems[3].data_group.concat(data_group3);
							o.newsitems[4].data_group = o.newsitems[4].data_group.concat(data_group4);
							o.newsitems[5].data_group = o.newsitems[5].data_group.concat(data_group5);
							o.loadingType = 0;//将loadingType归0重置
							uni.hideNavigationBarLoading();//关闭加载动画
						}
					});
				}
				// 限时 更多
				if(this.tabIndex1 == 2) {
					this.page2++;//每触底一次 page +1
					uni.request({
						url:'https://cx.mingmeijt.com/Api/AssembleOrder/index?p=' + this.page2,
						method: 'POST',
						data: {
							user_id: this.user_id,
							session_id: uni.getStorageSync("sessionid")
						},
						header:{'Content-Type':'application/x-www-form-urlencoded'},
						success: function(res) {
							if (res.data.data.order_list.list == '' || res.data.data.order_list.list == null) {//没有数据
								o.loadingType = 2;
								uni.hideNavigationBarLoading();//关闭加载动画
								return;
							}
							let data_limitTime = res.data.data.order_list.list
							for(var i = 0; i < data_limitTime.length; i++) {
								data_limitTime[i].goods_thumb_image = 'https://cx.mingmeijt.com' + data_limitTime[i].goods_thumb_image
								data_limitTime[i].goods_thumb = 'https://cx.mingmeijt.com' + data_limitTime[i].goods_thumb
							}
							let data_limitTime1 = data_limitTime.filter(function(item,index,array){
									return (item.status == "1" || item.status == 1);
							})
							let data_limitTime2 = data_limitTime.filter(function(item,index,array){
									return (item.status == "2" || item.status == 2);
							})
							let data_limitTime3 = data_limitTime.filter(function(item,index,array){
									return (item.status == "3" || item.status == 3);
							})
							let data_limitTime4 = data_limitTime.filter(function(item,index,array){
									return (item.status == "4" || item.status == 4);
							})
							let data_limitTime5 = data_limitTime.filter(function(item,index,array){
									return (item.status == "5" || item.status == 5);
							})
							o.newsitems[0].data_limitTime = o.newsitems[0].data_limitTime.concat(data_limitTime);
							o.newsitems[1].data_limitTime = o.newsitems[1].data_limitTime.concat(data_limitTime1);
							o.newsitems[2].data_limitTime = o.newsitems[2].data_limitTime.concat(data_limitTime2);
							o.newsitems[3].data_limitTime = o.newsitems[3].data_limitTime.concat(data_limitTime3);
							o.newsitems[4].data_limitTime = o.newsitems[4].data_limitTime.concat(data_limitTime4);
							o.newsitems[5].data_limitTime = o.newsitems[5].data_limitTime.concat(data_limitTime5);
							o.loadingType = 0;//将loadingType归0重置
							uni.hideNavigationBarLoading();//关闭加载动画
						}
					});
				}
			},
			// tab切换功能
			async changeTab(e) {
				let index = e.target.current;
				if (this.isClickChange) {
					this.tabIndex = index;
					this.isClickChange = false;
					return;
				}
				let tabBar = await this.getElSize("tab-bar"),
					tabBarScrollLeft = tabBar.scrollLeft;
				let width = 0;

				for (let i = 0; i < index; i++) {
					let result = await this.getElSize(this.tabBars[i].id);
					width += result.width;
				}
				let winWidth = uni.getSystemInfoSync().windowWidth,
					nowElement = await this.getElSize(this.tabBars[index].id),
					nowWidth = nowElement.width;
				if (width + nowWidth - tabBarScrollLeft > winWidth) {
					this.scrollLeft = width + nowWidth - winWidth;
				}
				if (width < tabBarScrollLeft) {
					this.scrollLeft = width;
				}
				this.isClickChange = false;
				this.tabIndex = index; //一旦访问data就会出问题
			},
			getElSize(id) { //得到元素的size
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select("#" + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				})
			},
			// 第一个tab栏点击
			async tapTab(e) {
				if (this.tabIndex == e.target.dataset.current) {
					return false;
				} else {
					this.isClickChange = true;
					this.tabIndex = e.target.dataset.current
				}
			},
			// 第二个tab栏点击
			async tapTab1(e) {
				let current = e.target.dataset.current
				if (this.tabIndex1 == e.target.dataset.current) {
					return false;
				} else {
					this.tabIndex1 = e.target.dataset.current
				}
			}
		}
	}
</script>

<style lang="scss">
	uni-page-body{height:100%;}
	.uni-swiper-tab {
		width: 100%;
		white-space: nowrap;
		line-height: 110upx;
		height: 120upx;
		border-bottom: 1px solid #c8c7cc;
		background: #fff;
	}
	.swiper-tab-list {
		font-size: 30upx;
		width: 150upx;
		display: inline-block;
		text-align: center;
		color: #555;
	}
	// 选中状态字体颜色
	.active {
		color: #007AFF;
	}
</style>